package com.sskj.linechat.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.OverScroller;

import androidx.annotation.Nullable;


public abstract class ScrollView extends View implements GestureDetector.OnGestureListener {


    private GestureDetector gestureDetector;

    private OverScroller overScroller;

    protected int mWidth;

    protected int mHeight;

    private boolean isTouched;

    //是否到顶
    private boolean isToTop = false;
    //是否到底
    private boolean isToBottom = false;



    public ScrollView(Context context) {
        this(context, null);
    }

    public ScrollView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        gestureDetector = new GestureDetector(getContext(), this);
        overScroller = new OverScroller(getContext());


    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        getParent().requestDisallowInterceptTouchEvent(true);
//        getParent().getParent().getParent().requestDisallowInterceptTouchEvent(true);
        gestureDetector.onTouchEvent(event);

        return true;
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        isTouched = true;
        return true;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        isTouched = false;
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float scrollX, float scrollY) {
        Log.e("TAG", "getScrollY(): " + getScrollY() + " scrollY: " + scrollY + " getBottomSlide: " +getBottomSlide());
        if (getScrollY() + scrollY < 0) {
            scrollY = -getScrollY();
            isToTop = true;
        }else {
            isToTop = false;
        }
        if (getScrollY() + scrollY > getBottomSlide()) {
            scrollY = getBottomSlide() - getScrollY();
            isToBottom = true;
        } else {
            isToBottom = false;
        }
        scrollBy(0, (int) scrollY);
        return true;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float velocityX, float velocityY) {
        overScroller.fling(0, getScrollY(), 0, (int) -velocityY, 0, 0, 0, Integer.MAX_VALUE);

        return true;
    }



    @Override
    public void computeScroll() {
        if (overScroller.computeScrollOffset()) {
            int y = overScroller.getCurrY();
            if (y < 0) {
                y = 0;
            }
            if (y > getBottomSlide()) {
                y = (int) (getBottomSlide());
            }
            scrollTo(0, y);
        }

    }


    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        Log.e("TAG", "scoll:" + t);
    }

    public abstract float getBottomSlide();

}
